package com.situ.blog.controller;

import com.situ.blog.pojo.entity.BlogType;
import com.situ.blog.pojo.query.BlogTypeQuery;
import com.situ.blog.service.IBlogTypeService;
import com.situ.blog.service.impl.BlogTypeServiceImpl;
import com.situ.blog.util.MyBeanUtils;
import com.situ.blog.util.PageInfo;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/blogType")
public class BlogTypeController extends BaseController{
    private IBlogTypeService blogTypeService = new BlogTypeServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        switch (method){
            case "selectByPage": //分页查询，搜索功能
                selectByPage(req,resp);
                break;
            case "selectAll":
                selectAll(req,resp);
                break;
            case "deleteById": //删除
                deleteById(req,resp);
                break;
            case "deleteAll":
                deleteAll(req,resp);
                break;
            case "add": //添加
                add(req,resp);
                break;
            case "selectById":
                selectById(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
        }
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeController.selectAll");
        List<BlogType> list = blogTypeService.selectAll();
        toJSON(resp,Result.ok(list));

    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeController.update");

        BlogType blogType = MyBeanUtils.copyToBean(req,BlogType.class);
        blogTypeService.update(blogType);
        toJSON(resp,Result.ok("更新成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeController.selectById");
        String id = req.getParameter("id");
        BlogType blogType = blogTypeService.seleteById(Integer.parseInt(id));
        toJSON(resp,Result.ok(blogType));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeController.add");

        BlogType blogType = MyBeanUtils.copyToBean(req, BlogType.class);
        blogTypeService.add(blogType);
        toJSON(resp,Result.ok("添加成功"));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        blogTypeService.deleteAll(ids);
        toJSON(resp,Result.ok("删除成功"));

    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeController.deleteById");
        String id = req.getParameter("id");
        blogTypeService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("BlogTypeController.selectByPage");
        BlogTypeQuery blogTypeQuery = MyBeanUtils.copyToBean(req,BlogTypeQuery.class);
        PageInfo pageInfo = blogTypeService.selectByPage(blogTypeQuery);
        toJSON(resp,pageInfo);
    }
}
